package com.simple.action.emp;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.simple.po.EmpEmployee;
import com.simple.service.common.SuggestCodeService;
import com.simple.service.emp.EmployeeService;
import com.xunj.action.common.AbstractAction;
import com.xunj.core.ParaUnit;
import com.xunj.core.SessionBean;

/**
 * 员工管理
 * @author 王瑾
 *
 */
public class EmployeeAction extends AbstractAction{

	private static final long serialVersionUID = 4654574303236951447L;
	private EmpEmployee empEmployee; //人员信息


	
	/**
	 * 查询
	 * @return
	 * @throws Exception
	 */
	public String listEmployee() throws Exception
	{
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
		
		paralist.add(new ParaUnit("state", "在用", ParaUnit.EQ));
		paralist.add(new ParaUnit("areaId", ParaUnit.ASC,ParaUnit.ORDER));
		paralist.add(new ParaUnit("employeeOrder", ParaUnit.ASC,ParaUnit.ORDER));
		
		if(POST())
		{
			if(!empEmployee.getEmployeeCode().equals(""))
				paralist.add(new ParaUnit("employeeCode", empEmployee.getEmployeeCode(), ParaUnit.LIKE));
			if(!empEmployee.getEmployeeName().equals(""))
				paralist.add(new ParaUnit("employeeName", empEmployee.getEmployeeName(), ParaUnit.LIKE));
			if(!empEmployee.getIdNumber().equals(""))
				paralist.add(new ParaUnit("idNumber", empEmployee.getIdNumber(), ParaUnit.LIKE));
			if(!empEmployee.getEmployeeGender().equals(""))
				paralist.add(new ParaUnit("employeeGender", empEmployee.getEmployeeGender(), ParaUnit.EQ));
			if(!empEmployee.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", empEmployee.getAreaId(), ParaUnit.EQ));
			if(!empEmployee.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", empEmployee.getAreaDeptId(), ParaUnit.EQ));
			if(!empEmployee.getSaleDeptId().equals(""))
				paralist.add(new ParaUnit("saleDeptId", empEmployee.getSaleDeptId(), ParaUnit.EQ));
		}
		
		List list = dao.criteriaByPage(EmpEmployee.class, paralist);

		request.setAttribute("datalist", list);
		return toJsp("/jsp/employee/employee/lst_employee.jsp");
	}
	/**
	 * 判断员工代码是否重复
	 * 
	 * @return
	 * @throws Exception
	 */
	public String checkExistsEmployee() throws Exception {
		String employeeCode = request.getParameter("employeeCode");
		String employeeId = request.getParameter("employeeId");
		String hql = "select count(*) from EmpEmployee where employeeCode='"+ employeeCode + "'";
		if(employeeId!=null)
			hql+=" and employeeId!='"+employeeId+"'";
		int employees = dao.findRowCount(hql);
		String data = "ok";
		if (employees > 0) {
			data = "exists";
		}
		request.setAttribute("data", data);
		return toResult();

	}
	/**
	 * 添加员工信息
	 * @return
	 * @throws Exception
	 */
	public String addEmployee() throws Exception
	{
		if(POST())
		{
			EmployeeService es = new EmployeeService(dao);
			//------------------基本信息add_employeeCallIn.jsp-----------------------
			HttpServletRequest request = ServletActionContext.getRequest();
			SessionBean sessionBean  = SessionBean.getSessionBean(request);
			empEmployee.setEmployeeId(UUID.randomUUID().toString());
			empEmployee.setState("在用");
			empEmployee.setEmployeeId(empEmployee.getEmployeeId());
			empEmployee.setCreateUserId(sessionBean.getUserId());
			empEmployee.setCreateUserName(sessionBean.getUserName());
			empEmployee.setCreateDate(new Date());

			dao.save(empEmployee);

			//更新快速检索码文件
			es.getEmpSuggestCode(this.getServletContext().getRealPath("common\\suggest\\emp.js"));
			
			this.Alert_GoUrl("保存成功", "listEmployee.do");
			return toMessage();
		}
		else
		{
			int newEmpCode = 0;
			String hql = "select max(cast(employeeCode as int)) from EmpEmployee";
			List codeList = dao.findAll(hql);
			if(!codeList.isEmpty())
			{
				Integer maxCode = (Integer) codeList.get(0);
				if(maxCode==null)
					maxCode=0;
				newEmpCode = maxCode+1;
			}
			request.setAttribute("employeeCode", newEmpCode);

			return toJsp("/jsp/employee/employee/add_employee.jsp");
		}
	}
	/**
	 * 修改员工信息
	 * @return
	 * @throws Exception
	 */
	public String updateEmployee() throws Exception
	{
		if(POST())
		{
			dao.update(empEmployee);
			
			EmployeeService es = new EmployeeService(dao);
			//更新快速检索码文件
			es.getEmpSuggestCode(this.getServletContext().getRealPath("common\\suggest\\emp.js"));
			this.Alert_GoUrl("保存成功", "listEmployee.do");
			return toMessage();
		}
		else
		{
			String employeeId = request.getParameter("employeeId");
			if(employeeId!=null && !"".equals(employeeId)){
				empEmployee = (EmpEmployee)dao.findOne(EmpEmployee.class, employeeId);
	
			}
			return toJsp("/jsp/employee/employee/update_employee.jsp");
		}
	}
	/**
	 * 删除员工信息
	 * @return
	 * @throws Exception
	 */
	public String deleteEmployee() throws Exception
	{
		String employeeId = request.getParameter("employeeId");
		empEmployee = (EmpEmployee)dao.findOne(EmpEmployee.class, employeeId);
		empEmployee.setState("删除");
		this.Alert_GoUrl("删除成功", "listEmployee.do");
		return toMessage();
	}
	/**
	 * 获取人员快速检索码
	 * @return
	 * @throws Exception
	 */
	public String getSuggestCodeEmployee() throws Exception {
		
		String employeeName=request.getParameter("employeeName");
		SuggestCodeService sService = new SuggestCodeService();
		String suggestCode = sService.getSuggestCode(employeeName);
		request.setAttribute("data", suggestCode);
		return toResult();
	}

	/**
	 * 员工信息通用查询
	 * @return
	 * @throws Exception
	 */
	public String listCommonEmployee() throws Exception
	{
		String jspPage = request.getParameter("jspPage");
		
		ArrayList<ParaUnit> paralist = new ArrayList<ParaUnit>();
		
		paralist.add(new ParaUnit("state", "在用", ParaUnit.EQ));
		paralist.add(new ParaUnit("areaId", ParaUnit.ASC,ParaUnit.ORDER));
		paralist.add(new ParaUnit("employeeOrder", ParaUnit.ASC,ParaUnit.ORDER));
		
		if(POST())
		{
			if(!empEmployee.getEmployeeCode().equals(""))
				paralist.add(new ParaUnit("employeeCode", empEmployee.getEmployeeCode(), ParaUnit.LIKE));
			if(!empEmployee.getEmployeeName().equals(""))
				paralist.add(new ParaUnit("employeeName", empEmployee.getEmployeeName(), ParaUnit.LIKE));
			if(!empEmployee.getIdNumber().equals(""))
				paralist.add(new ParaUnit("idNumber", empEmployee.getIdNumber(), ParaUnit.LIKE));
			if(!empEmployee.getEmployeeGender().equals(""))
				paralist.add(new ParaUnit("employeeGender", empEmployee.getEmployeeGender(), ParaUnit.EQ));
			if(!empEmployee.getAreaId().equals(""))
				paralist.add(new ParaUnit("areaId", empEmployee.getAreaId(), ParaUnit.EQ));
			if(!empEmployee.getAreaDeptId().equals(""))
				paralist.add(new ParaUnit("areaDeptId", empEmployee.getAreaDeptId(), ParaUnit.EQ));
			if(!empEmployee.getSaleDeptId().equals(""))
				paralist.add(new ParaUnit("saleDeptId", empEmployee.getSaleDeptId(), ParaUnit.EQ));
		}
		
		List list = dao.criteriaByPage(EmpEmployee.class, paralist);

		request.setAttribute("datalist", list);
		request.setAttribute("jspPage", jspPage);
		
		return toJsp(jspPage);
	}
	/**
	 * 查看员工信息
	 * @return
	 * @throws Exception
	 */
	public String showEmployee() throws Exception
	{
		String employeeId = request.getParameter("employeeId");
		if(employeeId!=null && !"".equals(employeeId)){
			empEmployee = (EmpEmployee)dao.findOne(EmpEmployee.class, employeeId);

		}
		return toJsp("/jsp/employee/employee/show_employee.jsp");
	}


	public EmpEmployee getEmpEmployee() {
		return empEmployee;
	}
	public void setEmpEmployee(EmpEmployee empEmployee) {
		this.empEmployee = empEmployee;
	}

	
}